package com.macasa.panel.entity.impl;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Version;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import org.hibernate.annotations.ForeignKey;

import com.google.common.base.Objects;
import com.macasa.panel.common.utils.CastHelpers;
import com.macasa.panel.entity.api.AbstractModel;
import com.macasa.panel.entity.api.IApacheServiceVhost;
import com.macasa.panel.entity.api.IDomain;
import com.macasa.panel.entity.api.ISubDomain;

@Entity
@Table(name = "sub_domain")
@SequenceGenerator(name = "SubDomain_SEQ", sequenceName = "SubDomainIdSeq", initialValue = 1)
@Getter
@Setter
@ToString
public class SubDomain extends AbstractModel<Integer> implements ISubDomain {

	/**
	 * The Constant serialVersionUID
	 */
	@Version
	private static final long	serialVersionUID	= 1885125740687303640L;

	@Id
	@Column(name = "sub_domain_id", nullable = false)
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "SubDomain_SEQ")
	private Integer				id;

	@ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY, targetEntity = com.macasa.panel.entity.impl.Domain.class)
	@JoinColumn(name = "fk_domain", nullable = false)
	private IDomain				domain;

	@Column(name = "sub_domain_name", nullable = false)
	private String				subDomainName;

	@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, targetEntity = com.macasa.panel.entity.impl.ApacheServiceVhost.class)
	@JoinColumn(name = "fk_apache_service_vhost", nullable = false)
	@ForeignKey(name = "fk_sub_domain_02")
	private IApacheServiceVhost	apacheServiceVhost;

	@Override
	public boolean equals(final Object obj) {

		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof SubDomain)) {
			return false;
		}

		final SubDomain other = CastHelpers.cast(obj, SubDomain.class);

		return Objects.equal(this.id, other.id) && Objects.equal(this.domain, other.domain) && Objects.equal(this.subDomainName, other.subDomainName)
				&& Objects.equal(this.apacheServiceVhost, other.apacheServiceVhost);
	}

	@Override
	public int hashCode() {
		return Objects.hashCode(this.id, this.domain, this.subDomainName, this.apacheServiceVhost);
	}
}
